package com.quanquan.management.service;

import DTO.PageDTO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.SelfHelpIntegralDTO;
import com.quanquan.management.entity.SysUser;
import com.quanquan.management.request.SelfHelpIntegralRequest;
import entity.ExcelData;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.DateUtils;
import utils.ExcelUtil;
import utils.Request;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName:SelfHelpIntegralService
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/4/15 21:02
 * @Version 1.0
 */
@Service
public class SelfHelpIntegralService {

    @Value("${module-url.userUrl}")
    private String userUrl;

    /**
     * 小票审核通过的列表
     * @param request
     * @return
     */
    public PageDTO<SelfHelpIntegralDTO> list(SelfHelpIntegralRequest request) {
        String json = Request.postJson(userUrl+"self_help_integral/list", request);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        PageDTO<SelfHelpIntegralDTO> pageDTO = null;
        if (0 == resCode) {
            JSONObject jsonData = jsonObject.getJSONObject("data");
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            JSONArray list = jsonData.getJSONArray("list");
            List<SelfHelpIntegralDTO> selfHelpIntegralDTOS = new LinkedList<>();
            for (int i = 0; i < list.size(); i++ ) {
                SelfHelpIntegralDTO object = list.getObject(i, SelfHelpIntegralDTO.class);
                selfHelpIntegralDTOS.add(object);
            }
            data.setList(selfHelpIntegralDTOS);
            return data;
        }
        return pageDTO;
    }


    /**
     * 待审核列表
     * @param request
     * @return
     */
    public PageDTO<SelfHelpIntegralDTO> checkList(SelfHelpIntegralRequest request) {
        String json = Request.postJson(userUrl+"self_help_integral/check_list", request);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        PageDTO<SelfHelpIntegralDTO> pageDTO = null;
        if (0 == resCode) {
            JSONObject jsonData = jsonObject.getJSONObject("data");
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            JSONArray list = jsonData.getJSONArray("list");
            List<SelfHelpIntegralDTO> selfHelpIntegralDTOS = new LinkedList<>();
            for (int i = 0; i < list.size(); i++ ) {
                SelfHelpIntegralDTO object = list.getObject(i, SelfHelpIntegralDTO.class);
                selfHelpIntegralDTOS.add(object);
            }
            data.setList(selfHelpIntegralDTOS);
            return data;
        }
        return pageDTO;
    }


    /**
     * 待审核列表(景枫特供)
     * @param request
     * @return
     */
    public PageDTO<SelfHelpIntegralDTO> checkListJF(SelfHelpIntegralRequest request) {

        String json = Request.postJson(userUrl+"self_help_integral/check_list_jf", request);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        PageDTO<SelfHelpIntegralDTO> pageDTO = null;
        if (0 == resCode) {
            JSONObject jsonData = jsonObject.getJSONObject("data");
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            JSONArray list = jsonData.getJSONArray("list");
            List<SelfHelpIntegralDTO> selfHelpIntegralDTOS = new LinkedList<>();
            for (int i = 0; i < list.size(); i++ ) {
                SelfHelpIntegralDTO object = list.getObject(i, SelfHelpIntegralDTO.class);
                selfHelpIntegralDTOS.add(object);
            }
            data.setList(selfHelpIntegralDTOS);
            return data;
        }
        return pageDTO;
    }


    /**
     * 审核记录列表
     * @param request
     * @return
     */
    public PageDTO<SelfHelpIntegralDTO> reviewResultList(SelfHelpIntegralRequest request) {
        String json = Request.postJson(userUrl+"self_help_integral/review_result_list", request);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        PageDTO<SelfHelpIntegralDTO> pageDTO = null;
        if (0 == resCode) {
            JSONObject jsonData = jsonObject.getJSONObject("data");
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            JSONArray list = jsonData.getJSONArray("list");
            List<SelfHelpIntegralDTO> selfHelpIntegralDTOS = new LinkedList<>();
            for (int i = 0; i < list.size(); i++ ) {
                SelfHelpIntegralDTO object = list.getObject(i, SelfHelpIntegralDTO.class);
                selfHelpIntegralDTOS.add(object);
            }
            data.setList(selfHelpIntegralDTOS);
            return data;
        }
        return pageDTO;
    }

    /**
     * 添加
     *
     * @param requestBody
     * @return
     */
    public int addSelfHelpIntegral(SelfHelpIntegralRequest requestBody) {

        if (null == requestBody) {
            return 3;
        }
        String json = Request.postJson(userUrl + "self_help_integral/add", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            String data = jsonObject.getString("data");
            if (null != data) {
                return 1;
            }
        }
        if (20002 == resCode) {

                return 2;
        }
        if (20003 == resCode) {
            return 4;
        }

        return 3;
    }


    public void export(HttpServletResponse response, SelfHelpIntegralRequest requestBody) throws Exception {
        List<String> titles = new LinkedList<>();
        titles.add("用户手机号");
        titles.add("用户名称");
        titles.add("小票号");
        titles.add("金额");
        titles.add("获得的积分");
        titles.add("商户名称");
        titles.add("商户业态");
        titles.add("消费日期");
        ExcelData data = ExcelUtil.getExcelData();
        data.setTitles(titles);
        PageDTO<SelfHelpIntegralDTO> pageDTO = this.list(requestBody);
        List<SelfHelpIntegralDTO> list = pageDTO.getList();
        List<List<Object>> rows = new LinkedList<>();
        list.forEach(vo -> {
            List<Object> row = new LinkedList<>();
            row.add(vo.getPhone());
            row.add(vo.getMemberName());
            row.add(vo.getTicketNum());
            row.add(vo.getMoney());
            row.add(vo.getReceivePoint());
            row.add(vo.getStoreName());
            row.add(vo.getStoreCommercial());
            row.add(vo.getConsumeDay());
            rows.add(row);
        });
        data.setRows(rows);
        String now = DateUtils.currentDate(DateUtils.DATE_HOUR_SECOND);
        ExcelUtil.exportExcel(response,"自助积分记录" + now,data);
    }

    public SelfHelpIntegralDTO findById(String id) {
        SelfHelpIntegralRequest requestBody = new SelfHelpIntegralRequest();
        requestBody.setSelfHelpIntegralId(id);
        String json = Request.postJson(userUrl + "self_help_integral/edit", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        SelfHelpIntegralDTO selfHelpIntegralDTO = null;
        if (0 == resCode) {
            JSONObject data = jsonObject.getJSONObject("data");
            selfHelpIntegralDTO = data.toJavaObject(SelfHelpIntegralDTO.class);
        }
        return selfHelpIntegralDTO;
    }

    public boolean update(SelfHelpIntegralRequest requestBody) {
        if (null == requestBody) {
            return false;
        }
        System.out.println(requestBody.getStoreId()+"商户id");
        String json = Request.postJson(userUrl + "self_help_integral/review", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }
}
